//index.js
//获取应用实例
const app = getApp()
var utils = require('../../utils/util.js');
Page({
    data: {
        currentTab: "all",
        navScrollLeft: 0,
        currentTab1: 0,
        navScrollLeft1: 0,
        ishide: 0,
        sc_id: "",
        number: 0
    },
    onShow: function (e) {
        var that = this;
        var shuzi = wx.getStorageSync("number") || 0;
        if (shuzi != 0) {
            wx.setTabBarBadge({
                index: 3,
                text: String(wx.getStorageSync("number"))
            })
        }
        that.lll();
        that.lll2();
    },
    onLoad: function () {
        var that = this;
        var shuzi = wx.getStorageSync("number") || 0;
        if (shuzi != 0) {
            wx.setTabBarBadge({
                index: 3,
                text: String(wx.getStorageSync("number"))

            })
        }
        wx.getSystemInfo({
            success: (res) => {
                this.setData({
                    pixelRatio: res.pixelRatio,
                    windowHeight: res.windowHeight,
                    windowWidth: res.windowWidth
                })
            },
        })
        that.categorylist();
        that.sliderlist();
        that.systemlist();
        that.activity();
        that.fine();
        that.spikeclass();
        that.salegoods();
        that.indexgoodslist()
        that.lll();
        that.lll2();
    },
    //分类商品切换接口
    categorylist: function (e) {
        var that = this;
        utils.http("/api/v1/category/category_list", {}, function (res) {
            that.setData({
                navData: res.datas
            })
        })
    },
    //轮播图接口   每日特价轮播 接口
    sliderlist: function (e) {
        var that = this;
        utils.http("/api/v1/common/slider_list", {}, function (res) {
            that.setData({
                one_slider: res.datas.one_slider,
                top_slider: res.datas.top_slider
            })
        })
    },
    //公告数据接口
    systemlist: function (e) {
        var that = this;
        utils.http("/api/v1/common/system", {}, function (res) {
            that.setData({
                systemData: res.datas
            })
        })
    },
    ////时令水果、圣诞来临、元旦到了、新年新气象接口
    activity: function (e) {
        var that = this;
        utils.http("/api/v1/index/activity_list", {}, function (res) {
            that.setData({
                activityData: res.datas
            })
        })
    },
    //精品选择接口
    fine: function (e) {
        var that = this;
        utils.http("/api/v1/index/fine_list", {}, function (res) {
            that.setData({
                fineData: res.datas
            })
        })
    },
    //秒杀分段  时间接口
    spikeclass: function (e) {
        var that = this;
        utils.http("/api/v1/index/spike_class_list", {}, function (res) {
            that.setData({
                spikeData: res.datas
            })
            if (res.datas.length>0) {
                that.spikegoods(res.datas[0].id);
            }
        })
    },
    //秒杀商品列表 接口
    spikegoods: function (scId) {
        var that = this;
        utils.http("/api/v1/index/spike_goods_list", {
            sc_id: scId
        }, function (res) {

            that.setData({
                spikegoodsData: res.datas
            })
        })
    },
    //热销商品 接口
    salegoods: function (e) {
        var that = this;
        utils.http("/api/v1/index/sale_goods_list", {}, function (res) {
            that.setData({
                salegoods: res.datas
            })
        })
    },

    //城市选择器
    onSwitchCityTap: function (e) {
        wx.navigateTo({
            url: '../switchcity/switchcity',
        })
    },
    switchNav(event) {
        var cur = event.currentTarget.dataset.current;
        var id = event.currentTarget.dataset.id;
        //每个tab选项宽度占1/5
        var singleNavWidth = this.data.windowWidth / 5;
        //tab选项居中                            
        this.setData({
            navScrollLeft: (cur - 2) * singleNavWidth
        })
        if (this.data.currentTab == cur) {
            return false;
        } else {
            this.setData({
                currentTab: cur
            })
        }
        this.ategoryGoodsList(id);
    },
    //点击分类全部
    switchNavAll: function (e) {
        var that = this;
        var all = "all";
        that.setData({
            currentTab: all
        })
    },
    switchNav1(event) {
        var cur = event.currentTarget.dataset.current;
        var id = event.currentTarget.dataset.id;
        var status = event.currentTarget.dataset.status;
        //每个tab选项宽度占1/5
        var singleNavWidth = this.data.windowWidth / 5;
        //tab选项居中                            
        this.setData({
            navScrollLeft1: (cur - 2) * singleNavWidth
        })
        if (this.data.currentTab1 == cur) {
            return false;
        } else {
            this.setData({
                currentTab1: cur,
                status: status
            })
        }
        this.spikegoods(id);
    },

    //时令水果、圣诞来临、元旦到了、新年新气象
    noWaresTap: function (e) {
        wx.navigateTo({
            url: '../solarclassification/solarclassification?id=' + e.currentTarget.dataset.id + "&banner=" + e.currentTarget.dataset.banner,
        })
    },
    //搜索
    onSearchTap: function (e) {
        wx.navigateTo({
            url: '../search/search',
        })
    },
    //商品详情
    noFrutisDetailsTap: function (e) {
        var id = e.currentTarget.dataset.id;
        wx.navigateTo({
            url: '../commoditydetails/commoditydetails?id=' + id,
        })
    },
    //附近好店
    onStorenearbyTap: function (e) {
        wx.navigateTo({
            url: '../storenearby/storenearby',
        })
    },
    //拼着更省
    onAllssembleTap: function (e) {
        wx.navigateTo({
            url: '../allssemble/allssemble',
        })
    },
    //一起众筹
    onCrowDraisingTap: function (e) {
        wx.navigateTo({
            url: '../crowdraising/crowdraising',
        })
    },
    //每日配送
    onDailydeLiveryTap: function (e) {
        wx.navigateTo({
            url: '../dailydelivery/dailydelivery',
        })
    },
    //进入公告详情
    oNnoticeDetailsTap: function (e) {
        wx.navigateTo({
            url: '../noticeDetails/noticeDetails',
        })
    },
    //跳入分类页面
    onClassifyTap: function (e) {
        wx.switchTab({
            url: '../classify/classify',
        })
    },
    //申请会员专享价
    onVipmemberTap:function(e){
        wx.switchTab({
            url: '../vipmember/vipmember',
        })
    },
    //添加购物车
    addCarTap: function (e) {
        var that = this;
        var shuzi = wx.getStorageSync("number") || 0;
        var number = shuzi + 1
        that.setData({
            number: number
        })
        wx.setStorageSync("number", number);
        //微信小程序 tabbar 上面设置消息数目等
        if (wx.getStorageSync("number") != 0) {
            wx.setTabBarBadge({
                index: 3,
                text: String(wx.getStorageSync("number"))

            })
        }
        var dataInfo = {
            id: e.currentTarget.dataset.id,
            image: e.currentTarget.dataset.image,
            name: e.currentTarget.dataset.name,
            price: e.currentTarget.dataset.price,
            subtitle: e.currentTarget.dataset.subtitle,
            vip_price: e.currentTarget.dataset.vipprice,
            selectStatus: false,   //购物车做删除时候用
            number: 1 //加减数量
        }
        utils.uniqueSearchRecord(dataInfo);
    },
    lll: function (e) {
        var that = this;
        if (that.data.indexgoods != undefined) {
            var indexgoods = that.data.indexgoods;
            for (var j in indexgoods) {
                for (var i in indexgoods[j].goods_list) {
                    indexgoods[j].goods_list[i].number = 0;
                    for (var k in wx.getStorageSync("listPost")) {
                        if (wx.getStorageSync("listPost")[k].id == indexgoods[j].goods_list[i].id) {
                            indexgoods[j].goods_list[i].number = wx.getStorageSync("listPost")[k].number;
                        }
                    }
                }
            }
            that.setData({
                indexgoods: indexgoods
            })
        }
    },
    lll2: function (e) {
        var that = this;
        if (that.data.ategoryList != undefined) {
            var ategoryList = that.data.ategoryList;
            for (var j in ategoryList) {
                for (var i in ategoryList[j].goods_list) {
                    ategoryList[j].goods_list[i].number = 0;
                    for (var k in wx.getStorageSync("listPost")) {
                        if (wx.getStorageSync("listPost")[k].id == ategoryList[j].goods_list[i].id) {
                            ategoryList[j].goods_list[i].number = wx.getStorageSync("listPost")[k].number;
                        }
                    }
                }
            }
            that.setData({
                ategoryList: ategoryList
            })
        }
    },
    //首页下方的商品列表不涉及分页
    indexgoodslist: function (e) {
        var that = this;
        utils.http("/api/v1/index/index_goods_list", {}, function (res) {
            for (var j in res.datas) {
                for (var i in res.datas[j].goods_list) {
                    res.datas[j].goods_list[i].number = 0;
                }
            }
            that.setData({
                indexgoods: res.datas
            })
            that.lll();
        })
    },
    //根据分类获取商品 
    ategoryGoodsList: function (categoryId) {
        var that = this;
        utils.showLoading();
        utils.http("/api/v1/goods/category_goods_list", {
            category_id: categoryId
        }, function (res) {
            utils.hideLoading();
            for (var j in res.datas) {
                for (var i in res.datas[j].goods_list) {
                    res.datas[j].goods_list[i].number = 0;
                }
            }
            that.setData({
                ategoryList: res.datas
            })
            that.lll2();
        })
    },
    //封装数据库加数据
    pervCtion: function (parameter) {
        var that = parameter.that;
        var itemslist = parameter.itemslist;
        var index = parameter.index;
        var ind = parameter.ind;
        var num = parameter.num;
        var shuzi = wx.getStorageSync("number") || 0;
        var number = shuzi + 1
        that.setData({
            number: number
        })
        wx.setStorageSync("number", number);
        //微信小程序 tabbar 上面设置消息数目等
        if (wx.getStorageSync("number") != 0) {
            wx.setTabBarBadge({
                index: 3,
                text: String(wx.getStorageSync("number"))
            })
        }
        var dataInfo = parameter.dataInfo;
        itemslist[ind]['goods_list'][index].number = itemslist[ind]['goods_list'][index].number + 1;
        utils.uniqueSearchRecord(dataInfo);
        return itemslist;

    },
    //封装数据库减数据
    nextCtion: function (parameter) {
        var that = parameter.that;
        var itemslist = parameter.itemslist;
        var index = parameter.index;
        var ind = parameter.ind;
        if (itemslist[ind]['goods_list'][index].number >0) {
            itemslist[ind]['goods_list'][index].number = itemslist[ind]['goods_list'][index].number - 1;
            var nowStorage = wx.getStorageSync("listPost");
            for (var i in nowStorage) {
                if (nowStorage[i].id == itemslist[ind]['goods_list'][index].id) {
                    nowStorage[i].number = nowStorage[i].number - 1;
                    if (nowStorage[i].number == 0) {
                        nowStorage.splice(i, 1);
                    }
                }
            }
            that.setData({
                number: wx.getStorageSync("number") - 1,
            })
            wx.setStorageSync("listPost", nowStorage)
            wx.setStorageSync("number", that.data.number);
            // //微信小程序 tabbar 上面设置消息数目等
            if (wx.getStorageSync("number") != 0) {
                wx.setTabBarBadge({
                    index: 3,
                    text: String(wx.getStorageSync("number"))
                })
            } else {
                wx.removeTabBarBadge({
                    index: 3
                })
            }
            return itemslist;
        } else {
            utils.message("该宝贝宝贝不能减少哦~");
        }
    },
    //加数量
    onPrevTap: function (e) {
        var that = this;
        var dataInfo = {
            id: e.currentTarget.dataset.id,
            image: e.currentTarget.dataset.image,
            name: e.currentTarget.dataset.name,
            price: e.currentTarget.dataset.price,
            subtitle: e.currentTarget.dataset.subtitle,
            vip_price: e.currentTarget.dataset.vipprice,
            selectStatus: false, //购物车做删除时候用
            number: 1 //加减数量
        }
        var parameter = {
            that: that,
            itemslist: that.data.indexgoods,
            index: e.currentTarget.dataset.index,
            ind: e.currentTarget.dataset.ind,
            dataInfo: dataInfo,
        }
        
        that.setData({
            indexgoods: that.pervCtion(parameter),
        })
    },
    //减数量
    onNextTap: function (e) {
        var that = this;
        var parameter = {
            that: that,
            itemslist: that.data.indexgoods,
            index: e.currentTarget.dataset.index,
            ind: e.currentTarget.dataset.ind,
        }
        that.setData({
            indexgoods: that.nextCtion(parameter),
        })
    },
    //加数量
    onPrevTap1: function (e) {
        var that = this;
        var dataInfo = {
            id: e.currentTarget.dataset.id,
            image: e.currentTarget.dataset.image,
            name: e.currentTarget.dataset.name,
            price: e.currentTarget.dataset.price,
            subtitle: e.currentTarget.dataset.subtitle,
            vip_price: e.currentTarget.dataset.vipprice,
            selectStatus: false, //购物车做删除时候用
            number: 1 //加减数量
        }
        var parameter = {
            that: that,
            itemslist: that.data.ategoryList,
            index: e.currentTarget.dataset.index,
            ind: e.currentTarget.dataset.ind,
            dataInfo: dataInfo,
        }
        that.setData({
            ategoryList: that.pervCtion(parameter),
        })
    },
    //减数量
    onNextTap1: function (e) {
        var that = this;
        var parameter = {
            that: that,
            itemslist: that.data.ategoryList,
            index: e.currentTarget.dataset.index,
            ind: e.currentTarget.dataset.ind,
        }
        that.setData({
            ategoryList: that.nextCtion(parameter),
        })
    },
})